package zom.zenjava.zendeploy.gui.swing;

import zom.zenjava.zendeploy.model.ApplicationProfile;
import zom.zenjava.zendeploy.model.Dependency;
import zom.zenjava.zendeploy.model.DependencySet;
import zom.zenjava.zendeploy.model.JreReference;

import javax.swing.*;
import java.awt.*;

public class ApplicationProfileView extends JPanel
{
    private JTextField nameField;
    private JTextField versionField;
    private JTextField jreField;
    private JList dependenciesList;
    private DefaultListModel dependenciesListModel;
    
    public ApplicationProfileView()
    {
        buildView();
    }

    public void setAppProfile(ApplicationProfile appProfile)
    {
        dependenciesListModel.removeAllElements();
        if (appProfile != null)
        {
            nameField.setText(appProfile.getName());
            versionField.setText(appProfile.getVersion());

            JreReference jreReference = appProfile.getJreReference();
            jreField.setText(jreReference != null ? jreReference.getVersion() : null);

            for (DependencySet dependencySet : appProfile.getDependencySet())
            {
                for (Dependency dependency : dependencySet.getDependencies())
                {
                    dependenciesListModel.addElement(dependency);
                }
            }
        }
        else
        {
            nameField.setText(null);
            versionField.setText(null);
            jreField.setText(null);
        }
    }
    
    private void buildView()
    {
        setLayout(new BorderLayout(10, 10));
        add(buildFieldArea(), BorderLayout.NORTH);
    }

    private Component buildFieldArea()
    {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(2, 2, 2, 2);

        // name

        gbc.gridx = 0;
        gbc.weightx = 0;
        panel.add(new JLabel("Name"), gbc);

        nameField = new JTextField(20);
        gbc.gridx = 1;
        gbc.weightx = 1;
        panel.add(nameField, gbc);

        // version

        gbc.gridy++;
        gbc.gridx = 0;
        gbc.weightx = 0;
        panel.add(new JLabel("Version"), gbc);

        versionField = new JTextField(20);
        gbc.gridx = 1;
        gbc.weightx = 1;
        panel.add(versionField, gbc);

        // jre

        gbc.gridy++;
        gbc.gridx = 0;
        gbc.weightx = 0;
        panel.add(new JLabel("JRE"), gbc);

        jreField = new JTextField(20);
        gbc.gridx = 1;
        gbc.weightx = 1;
        panel.add(jreField, gbc);

        // dependencies

        gbc.gridy++;
        gbc.gridx = 0;
        gbc.weightx = 0;
        panel.add(new JLabel("Dependencies"), gbc);

        dependenciesListModel = new DefaultListModel();
        dependenciesList = new JList(dependenciesListModel);
        dependenciesList.setCellRenderer(new DependencyRenderer());
        gbc.gridx = 1;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(new JScrollPane(dependenciesList), gbc);

        return panel;
    }

    //-------------------------------------------------------------------------
    
    private class DependencyRenderer extends DefaultListCellRenderer
    {
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Dependency)
            {
                Dependency dependency = (Dependency) value;
                setText(String.format("%s (%s)", dependency.getId(), dependency.getVersion()));
            }
            return this;
        }
    }
}
